package com.cuckoo.mall.thus.menucart;

import android.content.Context;

import com.bgn.baseframe.base.BasePresenterImpl;
import com.bgn.baseframe.network.HttpObserver;
import com.bgn.baseframe.network.bean.TDataBean;
import com.cuckoo.mall.javabean.CartItems;
import com.cuckoo.mall.javabean.CartProductItem;
import com.cuckoo.mall.network.ApiManager;

import java.util.List;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class MenucartPresenter extends BasePresenterImpl<MenucartContract.View> implements MenucartContract.Presenter {

    @Override
    public void refreshList() {
        ApiManager.loadCartList().subscribe(new HttpObserver<TDataBean<List<CartProductItem>>>(mView) {
            @Override
            public void onSucceed(TDataBean<List<CartProductItem>> value) {
                if (value.getData() != null && value.getData().size() > 0) {
                    mView.whenGetCartDatas(value.getData(), 3);
                } else {
                    mView.whenCartNoData();
                }

            }
        });
    }

    @Override
    public void loadCartList() {
        ApiManager.loadCartList().subscribe(new HttpObserver<TDataBean<List<CartProductItem>>>(mView) {
            @Override
            public void onSucceed(TDataBean<List<CartProductItem>> value) {
                if (value.getData() != null && value.getData().size() > 0) {
                    mView.whenGetCartDatas(value.getData(), 1);
                } else {
                    mView.whenCartNoData();
                }
            }
        });
    }

    @Override
    public void updateCartItemsNum(final int position, final int oldNum, final int cart_num, String cart_id) {
        ApiManager.updateCartItemNums(cart_id, cart_num).subscribe(new HttpObserver<TDataBean>(mView) {
            @Override
            public void onSucceed(TDataBean value) {
                mView.whenUpdateSucceed(position, cart_num);
            }

            @Override
            public void onDefeat(TDataBean value) {
                super.onDefeat(value);
                mView.whenUpdateNumFaild(position, oldNum);
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mView.whenUpdateNumFaild(position, oldNum);
            }
        });
    }

    @Override
    public void deleteCartItem(final List<Integer> positions, String cart_ids) {
        ApiManager.removeFromCart(cart_ids).subscribe(new HttpObserver<TDataBean>(mView) {
            @Override
            public void onSucceed(TDataBean value) {
                mView.whenDeleteSucceed(positions);
            }
        });
    }
}
